/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.service.fm.deployment;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;

import fr.jade.base.JadeBaseObject;
import fr.jade.util.FractalUtil;

public class BindTask extends JadeBaseObject implements DeploymentTask {

  protected NewComponentTask  clientTask;
  protected String            clientItfName;
  protected NewComponentTask  serverTask;
  protected String            serverItfName;
  
  public BindTask(NewComponentTask clientTask, String clientItfName, NewComponentTask serverTask, String serverItfName){
    assert clientTask != null        : "Client Task cannot be null";
    assert clientItfName != null     : "Client itf name cannot be null";
    assert !clientItfName.equals("") : "Client itf name cannot be empty";
    assert serverTask != null        : "Server Task cannot be null";
    assert serverItfName != null     : "Server itf name cannot be null";
    assert !serverItfName.equals("") : "Server itf name cannot be empty";
    
    this.clientTask=clientTask;
    this.clientItfName=clientItfName;
    this.serverTask=serverTask;
    this.serverItfName=serverItfName;
  }
  
  public void execute() throws DeploymentException {
    log("execute :" +toString());
    try{
    Component client = clientTask.getComponent();
    BindingController bc = FractalUtil.getBindingController(client);
    Component server = serverTask.getComponent();
    Object serverItf = server.getFcInterface(serverItfName);
    bc.bindFc(clientItfName, serverItf);
    }catch(NoSuchInterfaceException e){
      throw new DeploymentException("BindTask error", e);
    } catch (IllegalBindingException e) {
      throw new DeploymentException("BindTask error", e);
    } catch (IllegalLifeCycleException e) {
      throw new DeploymentException("BindTask error", e);
    }
  }

  public String getLevelName(){
    return "bind";
  }
  
  public String toString(){
    return 
      "BindTask[" +
      clientTask + "." + clientItfName + "->" +
      serverTask + "." + serverItfName + "]";
  }
}
